package com.wish.wishsystem_management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish.wishsystem_management.entity.SysDept;
import com.wish.wishsystem_management.entity.SysUser;
import com.wish.wishsystem_management.entity.TbBusiness;
import com.wish.wishsystem_management.mapper.TbBusinessMapper;
import com.wish.wishsystem_management.service.ISysDeptService;
import com.wish.wishsystem_management.service.ISysUserService;
import com.wish.wishsystem_management.service.ITbBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 商户表 服务实现类
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@Service
public class TbBusinessServiceImpl extends ServiceImpl<TbBusinessMapper, TbBusiness> implements ITbBusinessService {
    @Autowired
    ISysDeptService iSysDeptService;

    @Autowired
    ISysUserService iSysUserService;



    @Override
    public IPage list2(Page page) {
        return baseMapper.list2(page);
    }



    @Override
    public String saveCompanyInfo(TbBusiness company) {

        //=============================================商户商户商户商户商户===============================


//        TbBusiness businessEntity2;
//根据用户的电话查此用户
        SysUser user=iSysUserService.getSysUserByMobile(company.getMobile());
        Long a = user.getUserId();
        Integer b= Math.toIntExact(a.longValue());
        company.setUserId(b);  //Long 转Integer

        System.out.println("7788888查出的user ID需要返回这里"+user.getUserId());

//        saveCompanyInfo补全信息Start...！
        TbBusiness businessEntity =baseMapper.getBusinessByName(company.getName()); //调方法***1
        if(businessEntity != null){
            return "商户已经注册";
        }else{
//        saveCompanyInfo补全信息插入商户表Start
            company.setStatus(3);  //3为待审核

            company.setCreateTime(new Date());

//             businessEntity2=
            baseMapper.saveCompanyInfo1(company);//插入商户表      //调方法***2    //需要返回ID
//            saveCompanyInfo补全信息插入商户表END..
        }

//=============================================部门部门部门部门===============================
        SysDept sysDeptEntity  = new SysDept();    //需要返回ID

        sysDeptEntity.setName(company.getName());  //D1
        SysDept dept = iSysDeptService.getDeptByName(company.getName());  //调方法***3

        if(dept != null){
            return "商户已经注册";
        }else{
//            "saveCompanyInfo-->save补全信息插入部门表Start...！"
            sysDeptEntity.setParentId(1l);
            sysDeptEntity.setOrderNum(0);
            sysDeptEntity.setContacts(company.getCorporation());    //D2

            System.out.println("7788888增加后的商户ID需要返回这里"+company.getId());




            Integer a4= company.getId();  //增加后的商户ID需要返回这里   //D3
            Long b4=a4.longValue();
            sysDeptEntity.setBusinessId(b4);


//            SysDept dept2=
            iSysDeptService.save2(sysDeptEntity);//插入部门表      //调方法***4

        }

//===============================用户用户用户用户用户用户用户=============================================

        SysUser sysUserEntity = new SysUser();
        sysUserEntity.setApprovalFlag(3);


        //下面为省市县的赋值
        if(company.getProvince() != null || !("".equals(company.getProvince()))){
            sysUserEntity.setProvince(String.valueOf(company.getProvince()));
        }
        if(company.getCity() != null || !("".equals(company.getCity()))){
            sysUserEntity.setCity(String.valueOf(company.getCity()));
        }
        if(company.getCounty()!= null || !("".equals(company.getCounty()))){
            sysUserEntity.setCountry(String.valueOf(company.getCounty()));
        }


//       saveCompanyInfo-->update补全信息更新用户表Start

        System.out.println("7788888增加后的部门ID需要返回这里"+sysDeptEntity.getDeptId());


        sysUserEntity.setDeptId(sysDeptEntity.getDeptId());  //增加后的部门ID需要返回这里
        sysUserEntity.setUserId(company.getUserId().longValue());  //U3
        sysUserEntity.setCompanyName(company.getName());   //U4
        //sysUserEntity.setContactMember(company.getContactMember());
        sysDeptEntity.setContacts(company.getCorporation());   //U5
        sysUserEntity.setUsername(company.getName());  //U6

        iSysUserService.update3(sysUserEntity);//更新用户                 //调方法***5



        return "666";
    }



}
